package cc.mycraft.pvp;

import java.io.File;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

public class PlayerManager {
	private static File PlayerDataFile;
	private static FileConfiguration PData;
	protected static File PlayerPointFile;
	protected static FileConfiguration PPoint;
	
	
	public static void run() {
		PlayerDataFile = new File(Main.getPlugin().getDataFolder()+"/PlayerData.yml");
		PData = YamlConfiguration.loadConfiguration(PlayerDataFile);
		PlayerPointFile = new File(Main.getPlugin().getDataFolder()+"/PlayerPoints.yml");
		PPoint = YamlConfiguration.loadConfiguration(PlayerPointFile);
	}
	public static void savePData() {
		try {
			PData.save(PlayerDataFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void savePPoint() {
		try {
			PPoint.save(PlayerPointFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static String PlayerSection(Player p) {
		return p.getName()+"__"+p.getUniqueId();
	}
	public static void getInfo(Player p) {
		WeeklyReward.checkWeeklyReward(p);
		int Kill = PData.getInt(PlayerSection(p)+".Kill");
		int Death = PData.getInt(PlayerSection(p)+".Death");
		int MaxCombo = PData.getInt(PlayerSection(p)+"Combo");
		double KD = (double)((int)(((double)Kill/(double)Death)*100))/100;
		int Points = PPoint.getInt("Points."+PlayerSection(p));
		p.sendMessage("§e你的战绩：\n§e击败："+Kill+"   死亡："+Death+"  分数："+Points+"  击败/死亡："+KD+"  最高连胜："+MaxCombo);
	}
	public static void addKillAmount(Player p,int Amount) {
		PData.set(PlayerSection(p)+".Kill", PData.getInt(PlayerSection(p)+".Kill")+Amount);
		savePData();
	}
	public static int getKillAmount(Player p) {
		return PData.getInt(PlayerSection(p)+".Kill");
	}
	public static void setKillAmount(Player p,int Amount) {
		PData.set(PlayerSection(p)+".Kill",Amount);
		savePData();
	}
	public static void addDeathAmount(Player p,int Amount) {
		PData.set(PlayerSection(p)+".Death", PData.getInt(PlayerSection(p)+".Death")+Amount);
		savePData();
	}
	public static int getDeathAmount(Player p) {
		return PData.getInt(PlayerSection(p)+".Death");
	}
	public static void setDeathAmount(Player p,int Amount) {
		PData.set(PlayerSection(p)+".Death",Amount);
		savePData();
	}
	public static void setMaxCombo(Player p,int Amount) {
		PData.set(PlayerSection(p)+".MaxCombo",Amount);
		savePData();
	}
	public static int getMaxCombo(Player p) {
		return PData.getInt(PlayerSection(p)+".MaxCombo");
	}
	public static void addPoint(Player p,int Amount) {
		PPoint.set("Points."+PlayerSection(p), PPoint.getInt(PlayerSection(p))+Amount);
		savePPoint();
	}
	public static void subPoint(Player p,int Amount) {
		int point = PPoint.getInt("Points."+PlayerSection(p))-Amount;
		if(point<0) {
			point = 0;
		}
		PPoint.set("Points."+PlayerSection(p), point);
		savePPoint();
	}
	public static void setPoint(Player p,int Amount) {
		PPoint.set("Points."+PlayerSection(p), Amount);
		savePPoint();
	}
	public static int getPoint(Player p) {
		return PPoint.getInt("Points."+PlayerSection(p));
	}
	public static void clearPoint() {
		PPoint.set("Points", null);
		try {
			PPoint.save(PlayerPointFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		PlayerPointFile = new File(Main.getPlugin().getDataFolder()+"/PlayerPoints.yml");
		PPoint = YamlConfiguration.loadConfiguration(PlayerPointFile);
	}
}
